﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CodeDemon.Models;
using System.Web.Security;
using CodeDemon.Utilities;

namespace CodeDemon.Controllers {
	public class UserController : ControllerBase {

		[RequiresAuthentication]
		public ActionResult DisplayUserList(bool multi, string cssId, string noSelf)
		{
			if (noSelf != null)
			{
				// Add action logic here
				ReviewModel model = new ReviewModel();
				// TODO: This is a hack
				// HACK: omfg
				model.UserSelectCSS_ID = cssId;
				PopulateModel(model);
				if (multi)
				{
					return View("DisplayUserListMultiNoSelf", model);
				}
				else
				{
					return View("DisplayUserListNoSelf", model);
				}
			}
			else
			{

				// Add action logic here
				ReviewModel model = new ReviewModel();
				// TODO: This is a hack
				// HACK: omfg
				model.UserSelectCSS_ID = cssId;
				PopulateModel(model);
				if (multi)
				{
					return View("DisplayUserListMulti", model);
				}
				else
				{
					return View("DisplayUserList", model);
				}
			}
		}

		public ActionResult DisplayLoginForm(string ReturnUrl)
		{
			GenericActionModel model = new GenericActionModel();
			model.VarHash.Add("ReturnURL", ReturnUrl);
			return View( "DisplayLoginForm" ,model);
		}
		

		public ActionResult ProcessLoginRequest(string username, string password, string ReturnUrl)
		{
			try
			{
				ReviewModel model = new ReviewModel();
				var user = (from users in model.DAO.GetUserByEmail(username) select users).FirstOrDefault();
				string salt = user.Cryptedpassword.Substring(0, 2);
				string cryptOut = UnixCrypt.Crypt(salt, password);
				if (cryptOut == user.Cryptedpassword)
				{
					// TODO: User SetAuthCookie() and do a manual redirect to RedirectURL
					FormsAuthentication.RedirectFromLoginPage(username, true);
					GenericActionModel model2 = new GenericActionModel();
					model2.ErrorMessage = "Authentication Failed";
					return View("DisplayLoginForm", model2);
				}
				else
				{
					GenericActionModel model2 = new GenericActionModel();
					model2.ErrorMessage = "Authentication Failed";
					return View("DisplayLoginForm", model2);
				}
			}
			catch (System.Exception)
			{
				GenericActionModel model2 = new GenericActionModel();
				model2.ErrorMessage = "Authentication Failed, please check your username and password.";
				return View("DisplayLoginForm", model2);

			}
		}
		[RequiresAuthentication]
		public ActionResult LogoutUser() {
			FormsAuthentication.SignOut();
			Response.Clear();
			return Redirect("/CodeDemon/Home/Index");
		}

		[RequiresAuthentication]
		public ActionResult DisplayUserProfile()
		{
			ReviewModel model = new ReviewModel();
			PopulateModel(model);
			return View("DisplayUserProfile", model);
		}

		[RequiresAuthentication]
		public ActionResult UpdateUserProfile(string create, string status, string comment, string join, string other_join)
		{
			bool createB = false; bool statusB = false; bool commentB = false; bool joinB = false; bool other_joinB = false;

			if (create != null && create == "True")
			{
				createB = true;
			}

			if (status != null && status == "True")
			{
				statusB = true;
			}
			if (comment != null && comment == "True")
			{
				commentB = true;
			}
			if (join != null && join == "True")
			{
				joinB = true;
			}
			if (other_join != null && other_join == "True")
			{
				other_joinB = true;
			}

			ReviewModel model = new ReviewModel();
			PopulateModel(model);
			model.UpdateReviewUserSettings(createB, statusB, commentB, joinB, other_joinB, CurrentLoggedInReviewUserID);
			return View("DisplayUserProfile", model); 
		}
	}
}
